package com.gitee.baqkft.code.ui.dlg;

import com.gitee.baqkft.code.PdfImagesConvertor;
import com.gitee.baqkft.code.ui.layout.GBC;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.concurrent.CompletableFuture;

/**
 * PDF工具集
 * @Author: chenmingcai
 * @Date 2023/10/29 17:27
 **/

public class PDFToolsFrame extends JFrame
{
    /**
     * PDF框
     */
    private JTextField pdfField;

    /**
     * 存储位置
     */
    private JTextField saveDirField;

    /**
     * 进度条
     */
    private JProgressBar bar;

    public PDFToolsFrame()
    {
        initUI();
    }

    /**
     * 初始化界面
     */
    private final void initUI()
    {
        setLayout(new GridBagLayout());
        setSize(600, 250);

        JPanel browsPanel = createChoosePDFPanel();
        GBC gbc = new GBC(0, 0, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        browsPanel.setBorder(BorderFactory.createTitledBorder("PDF文件"));
        add(browsPanel, gbc);

        JPanel savePanel = createTargetSaveDirPanel();
        gbc = new GBC(0, 1, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        savePanel.setBorder(BorderFactory.createTitledBorder("存储位置"));
        add(savePanel, gbc);

        final JButton btnConvert = new JButton("转换成图片");
        gbc = new GBC(0, 2, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        add(btnConvert, gbc);
        btnConvert.addActionListener(this::convertPdfToImages);

        bar = new JProgressBar();
        gbc = new GBC(0, 3, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        add(bar, gbc);
        bar.setVisible(false);
        bar.setStringPainted(true);
    }

    /**
     * 创建PDF文件浏览框
     * @return
     */
    private final JPanel createChoosePDFPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        JLabel label = new JLabel("选择PDF:");
        GBC gbc = new GBC(0, 0, 1, 1);
        gbc.setWeight(0, 0);
        panel.add(label, gbc);

        pdfField = new JTextField();
        gbc = new GBC(1, 0, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        panel.add(pdfField, gbc);

        JButton browse = new JButton("选择");
        gbc = new GBC(2, 0, 1, 1);
        gbc.setWeight(0, 0);
        panel.add(browse, gbc);
        browse.addActionListener(this::choosePDFFile);

        return panel;
    }

    /**
     * 选择PDF文件
     * @param event
     */
    private void choosePDFFile(ActionEvent event)
    {
        JFileChooser chooser = new JFileChooser();
        File file = new File(pdfField.getText().trim());
        if(file.exists())
        {
            chooser = new JFileChooser(pdfField.getText());
        }

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF(*.pdf)", "pdf");
        chooser.setFileFilter(filter);

        if(JFileChooser.APPROVE_OPTION == chooser.showDialog(this, "选择PDF源"))
        {
            String dir = chooser.getSelectedFile().getPath();
            file = new File(dir);
            if(!file.exists())
            {
                int idx = dir.lastIndexOf(File.separator);
                dir = dir.substring(0, idx);
            }
            pdfField.setText(dir);
        }
    }

    /**
     * 创建目录保存目录
     * @return
     */
    private final JPanel createTargetSaveDirPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        JLabel label = new JLabel("图存存储位置:");
        GBC gbc = new GBC(0, 0, 1, 1);
        gbc.setWeight(0, 0);
        panel.add(label, gbc);

        saveDirField = new JTextField();
        gbc = new GBC(1, 0, 1, 1);
        gbc.setWeight(1, 0).setFill(GBC.HORIZONTAL);
        panel.add(saveDirField, gbc);

        JButton browse = new JButton("选择");
        gbc = new GBC(2, 0, 1, 1);
        gbc.setWeight(0, 0);
        panel.add(browse, gbc);
        browse.addActionListener(this::chooseSaveDir);

        return panel;
    }

    /**
     * 目录存储位置
     */
    private void chooseSaveDir(final ActionEvent event)
    {
        JFileChooser chooser = new JFileChooser();
        File file = new File(saveDirField.getText().trim());
        if(file.exists())
        {
            chooser = new JFileChooser(saveDirField.getText());
        }

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if(JFileChooser.APPROVE_OPTION == chooser.showDialog(this, "存储到这"))
        {
            String dir = chooser.getSelectedFile().getPath();
            file = new File(dir);
            if(!file.exists())
            {
                int idx = dir.lastIndexOf(File.separator);
                dir = dir.substring(0, idx);
            }
            saveDirField.setText(dir);
        }
    }

    /**
     * 将PDF转换成图片
     * @param event
     */
    private void convertPdfToImages(final ActionEvent event)
    {
        final File srcFile = new File(pdfField.getText().trim());
        if(!srcFile.exists() || !srcFile.isFile())
        {
            JOptionPane.showConfirmDialog(this, "请选择正确的PDF文件");
            return;
        }

        final String dir = saveDirField.getText().trim();
        final File targetDir = new File(dir);
        if(!targetDir.exists())
            targetDir.mkdirs();
        else if(targetDir.isFile())
        {
            JOptionPane.showConfirmDialog(this, "目录存储位置已被文件占用，请重新选择存储位置");
            return;
        }

        CompletableFuture.runAsync(new PdfImagesConvertor(srcFile, dir, bar));
    }
}
